package com.example.handlermodule

import android.app.Activity
import android.os.*
import android.view.View
import android.widget.EditText
import android.widget.ProgressBar
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.io.BufferedReader
import java.io.ByteArrayOutputStream
import java.io.InputStreamReader
import java.lang.StringBuilder
import java.net.HttpURLConnection
import java.net.URL
import java.util.concurrent.ExecutorService
import java.util.concurrent.SynchronousQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import javax.net.ssl.HttpsURLConnection
import kotlin.concurrent.thread


/**
 * 测试Handler的基本使用
 * @author 张晓飞
 * 1. 创建Handler成员变量对象, 并重写其handleMessage()
 * 2. 在分/主线程创建Message对象
 * 3. 使用handler对象发送Message
 * 4. 在handleMessage()中处理消息
 */
class HandlerTestActivity : Activity() {
    private  lateinit var pb_handler1_loading: ProgressBar
    private lateinit var et_handler1_result: EditText
    val updateText = 1
    val handler = object:Handler(Looper.getMainLooper()){
        override fun handleMessage(msg: Message) {

            //在这里可以进行UI操作
            when(msg.what){
                updateText-> {
                    val result=msg.obj as String
                    et_handler1_result.setText(result)
                    pb_handler1_loading.visibility=View.INVISIBLE
                }
            }
        }
    }

    fun testOkHttp(){

    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_handler_test)
        pb_handler1_loading = findViewById<View>(R.id.pb_handler1_loading) as ProgressBar
        et_handler1_result = findViewById<View>(R.id.et_handler1_result) as EditText
    }

    fun getSubmit1(view: View) {
        this.pb_handler1_loading.visibility=View.VISIBLE
        val response = StringBuilder()
        thread {
            runOnUiThread {  }

            val URL = URL("https://gitee.com")
            val connection=URL.openConnection() as HttpsURLConnection
            connection.connectTimeout=8000
            connection.readTimeout=8000
            val inputstream = connection.inputStream
            val bufferedReader = BufferedReader(InputStreamReader(inputstream))
            var len = 0;
            bufferedReader.use {
                bufferedReader.forEachLine {
                    response.append(it)
                }
            }
            runOnUiThread {
                this.et_handler1_result.setText(response.toString())

                this.pb_handler1_loading.visibility=View.INVISIBLE
            }
        }
    }


    fun getSubmit2(view: View) {
        //测试handler的使用步骤
        this.pb_handler1_loading.visibility=View.VISIBLE
        val response = StringBuilder()
        thread {
            val URL = URL("https://gitee.com")
            val connection=URL.openConnection() as HttpsURLConnection
            connection.connectTimeout=8000
            connection.readTimeout=8000
            val inputstream = connection.inputStream
            val bufferedReader = BufferedReader(InputStreamReader(inputstream))
            var len = 0;
            bufferedReader.use {
                bufferedReader.forEachLine {
                    response.append(it)
                }
            }


            //使用handlerg更新UI
            val message = Message.obtain()
            message.what = updateText
            message.obj=response.toString()

            handler.sendMessage(message)
        }
    }

    fun getSubmit3(view: View) {
        //测试handler的使用步骤
        this.pb_handler1_loading.visibility=View.VISIBLE
        val response = StringBuilder()
        val mLooper=mainLooper
        mLooper.quit()
        
    }

}
fun main(){
//    val syncqueue = SynchronousQueue<String>()
////    val executor = ThreadPoolExecutor(0,Int.MAX_VALUE,60L,TimeUnit.SECONDS,SynchronousQueue<Runnable>())
//    thread {
//        syncqueue.put("1")
//        syncqueue.put("1")
//    }
//    Thread.sleep(1000)
//    thread {
//        println(syncqueue.take())
//        Thread.sleep(500)
//        println(syncqueue.take())
//    }
//    syncqueue.put("2")

    runBlocking {
        val result=async {
            5+5
        }.await()
        println(result)
    }
}
